package cn.wuzhixiang.music

import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import cn.wuzhixiang.music.ui.components.layout.AppBottomBar
import cn.wuzhixiang.music.ui.models.OrderViewModel
import cn.wuzhixiang.music.ui.screens.HomeScreen
import cn.wuzhixiang.music.ui.screens.MineScreen

/**
 * enum values that represent the screens in the app
 */
enum class AppScreens(val title: String) {
    Home(title = "首页"),
    Mine(title = "我的")
}


@Composable
fun App(
    viewModel: OrderViewModel,
    navController: NavHostController = rememberNavController()
) {
    // Get current back stack entry
    val backStackEntry by navController.currentBackStackEntryAsState()
    // Get the name of the current screen
    val currentScreen = AppScreens.valueOf(
        backStackEntry?.destination?.route ?: AppScreens.Home.name
    )

    Scaffold(
        bottomBar = {
            AppBottomBar(onClick = { name ->
//                navController.navigate(name)
                // 打印出name
                Log.d("name", name)
                navController.navigate(name)
            })
        }
    ) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = AppScreens.Home.name,
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(innerPadding)
        ) {
            composable(route = AppScreens.Home.name) {
                HomeScreen(
                    onClickGoMine = {
                        navController.navigate(AppScreens.Mine.name)
                    }
                )
            }
            composable(route = AppScreens.Mine.name) {
                MineScreen()
            }
        }
    }
}

/**
 * Resets the [OrderUiState] and pops up to [CupcakeScreen.Start]
 */
private fun cancelOrderAndNavigateToStart(
    viewModel: OrderViewModel,
    navController: NavHostController
) {
    viewModel.resetOrder()
    navController.popBackStack(AppScreens.Home.name, inclusive = false)
}

/**
 * Creates an intent to share order details
 */
private fun shareSomething(context: Context, subject: String, summary: String) {
    // Create an ACTION_SEND implicit intent with order details in the intent extras
    val intent = Intent(Intent.ACTION_SEND).apply {
        type = "text/plain"
        putExtra(Intent.EXTRA_SUBJECT, subject)
        putExtra(Intent.EXTRA_TEXT, summary)
    }
    context.startActivity(
        Intent.createChooser(
            intent,
            "哈哈哈哈"
        )
    )
}